//
//  PublicView.m
//  AlarmClock
//
//  Created by apple on 16/9/29.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "PublicView.h"
#import "SDAutoLayout.h"
#import "PublicTableViewCell.h"
#import "MultiSelectedView.h"
#import "BSKConsts.h"
@interface PublicView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UIView *backView;
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIButton *cancelButton;
@property (nonatomic,strong) NSArray *dataSource;
@property (nonatomic,strong) NSArray *repeatArray;
@end
@implementation PublicView

- (void)show:(CGRect)rect title:(NSString *)title dataSource:(NSArray *)dataSource {
    [self addSubview:self.backView];
    self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.51];
    self.backView.frame = rect;
    self.backView.backgroundColor = [UIColor whiteColor];
    self.dataSource = dataSource;
    self.titleLabel.text = title;
    
    self.alpha = 0;
    self.backView.transform = CGAffineTransformMakeScale(0.1, 0.1);
    
    [self.backView addSubview:self.titleLabel];
    [self.backView addSubview:self.tableView];
    [self.backView addSubview:self.cancelButton];
    
    self.titleLabel.sd_layout
    .topSpaceToView(self.backView,10)
    .leftSpaceToView(self.backView,0)
    .widthIs(rect.size.width)
    .heightIs(30);
    
    self.tableView.sd_layout
    .leftSpaceToView(self.backView,0)
    .rightSpaceToView(self.backView,0)
    .topSpaceToView(self.titleLabel,0)
    .heightIs(rect.size.height - 110);
    
    self.cancelButton.sd_layout
    .leftSpaceToView(self.backView,20)
    .rightSpaceToView(self.backView,20)
    .bottomSpaceToView(self.backView,10)
    .heightIs(rect.size.width / 6);
   
    
    [UIView animateWithDuration:0.4 animations:^{
        self.backView.transform = CGAffineTransformMakeScale(1, 1);
        self.alpha = 1;
    }];

}
-(void)removeSelf{
    [UIView animateWithDuration:0.2 animations:^{
        self.backView.transform = CGAffineTransformMakeScale(0.1, 0.1);
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch * touch = [[event touchesForView:self] anyObject];
    if (touch) {
        [UIView animateWithDuration:0.3 animations:^{
            self.backView.transform = CGAffineTransformMakeScale(0.1, 0.1);
            self.alpha = 0;
        } completion:^(BOOL finished) {
            [self removeFromSuperview];
        }];
    }
    
}

#pragma mark - action
- (void)cancelButtonPressed:(UIButton *)sender {
    [UIView animateWithDuration:0.3 animations:^{
        self.backView.transform = CGAffineTransformMakeScale(0.1, 0.1);
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


#pragma mark - delegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"view";
    PublicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[PublicTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.titleLabel.text = self.dataSource[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
    if (_selectedCallback) {
        _selectedCallback(indexPath.row,self);
    }
}

#pragma mark - setter

-(void)setDefaulteSelectedIndex:(NSInteger)defaulteSelectedIndex{
    [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:defaulteSelectedIndex inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
    _defaulteSelectedIndex = defaulteSelectedIndex;
}

#pragma mark - getter
- (UIView *)backView {
    if (_backView) {
        return _backView;
    }
    _backView = [[UIView alloc]init];
    _backView.layer.masksToBounds = YES;
    _backView.layer.cornerRadius = 10;
    return _backView;
}
- (UILabel *)titleLabel {
    if (_titleLabel) {
        return _titleLabel;
    }
    _titleLabel = [[UILabel alloc]init];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    return _titleLabel;
}

- (UITableView *)tableView {
    if (_tableView) {
        return _tableView;
    }
    _tableView = [[UITableView alloc]init];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.allowsMultipleSelection = NO;
    _tableView.rowHeight = 70;
    return _tableView;
}

- (UIButton *)cancelButton {
    if (_cancelButton) {
        return _cancelButton;
    }
    _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancelButton = [[UIButton alloc]init];
    _cancelButton.backgroundColor = [UIColor redColor];
    _cancelButton.layer.masksToBounds = YES;
    _cancelButton.layer.cornerRadius = 10;
    [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [_cancelButton addTarget:self action:@selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    return _cancelButton;
}
@end
